**** Code for preparing the NELDA data for analysis
**** Author: Ryan Kennedy
**** Dependencies: Stata (Prepared in Version 13, but should be backward compatible)

* Check to see how many cases we have and the proportion of outcomes
codebook nelda24 if types == "Executive" & (ccode == 160 | ccode == 80 | ccode == 145 | ccode == 140 | ccode == 155 | ccode == 100 | ccode == 94 | ccode == 130 | ccode == 92 | ccode == 90 | ccode == 110 | ccode == 91 | ccode == 70 | ccode == 93 | ccode ==95 | ccode == 150 | ccode == 135 | ccode == 115 | ccode == 165 | ccode == 101)

* Narrow to Executive elections
keep if types == "Executive"

* DV did incumbent party win
gen incWin = 1 if nelda24 == "no"
replace incWin = 0 if nelda24 == "yes"
label variable incWin "Did incumbent party win"

* IVs
** Was previous election suspended?
gen electSuspend = 1 if nelda1 == "yes"
replace electSuspend = 0 if electSuspend == .
label variable electSuspend "Was previous regular election suspended"

** First multiparty elections?
gen multPart1 = 1 if nelda2 == "yes"
replace multPart1 = 0 if multPart1 == .
label variable multPart1 "First multiparty elections"

** Opposition allowed
gen oppAllow = 1 if nelda3 == "yes"
replace oppAllow = 0 if oppAllow == .
label variable oppAllow "Opposition allowed"

** More than one party legal
gen multLegal = 1 if nelda4 == "yes"
replace multLegal = 0 if multLegal == .
label variable multLegal "More than one party legal"

** Choice of candidate on the ballot
gen multChoice = 1 if nelda5 == "yes"
replace multChoice = 0 if multChoice == .
label variable multChoice "Choice of candidates on the ballot"

** Were elections earlier or later than scheduled
gen manipTime = 1 if nelda6 == "yes"
replace manipTime = 0 if manipTime == .
label variable manipTime "Was timing of the election earlier or later than scheduled"

** Indications incumbent made a prior decision to give up power
gen incCede = 1 if nelda7 == "yes"
replace incCede = 0 if incCede == .
label variable incCede "Incumbent indicated decision to step down"

** Incumbent reached term limit
gen incLimit = 1 if nelda8 == "yes"
replace incLimit = 0 if incLimit == .
label variable incLimit "Incumbent reached term limit"

** Had incumbent extended term in office or eligibility
gen incExtend = 1 if nelda9 == "yes"
replace incExtend = 0 if incExtend == .
label variable incExtend "incumbent extended term or eligibility"

** Transitional leadership tasked with holding elections
gen transLead = 1 if nelda10 == "yes"
replace transLead = 0 if transLead == .
label variable transLead "transitional leadership tasked elections"

** Significant concerns elections will not happen.
gen concCan = 1 if nelda11 == "yes"
replace concCan = 0 if concCan == .
label variable concCan "concerns election not happen"

** Incumbent confident of victory
gen incConf = 1 if nelda12 == "yes"
replace incConf = 0 if incConf == .
label variable incConf "incumbent confident of victory before"

** Opposition leaders prevented from running
gen oppPrev = 1 if nelda13 == "yes"
replace oppPrev = 0 if oppPrev == .
label variable oppPrev "opposition leaders prevented from running"

** Opposition boycott
gen oppBoycott = 1 if nelda14 == "yes"
replace oppBoycott = 0 if oppBoycott == .
label variable oppBoycott "Opposition boycott of election"

** Government harassed opposition
gen oppHarass = 1 if nelda15 == "yes"
replace oppHarass = 0 if oppHarass == .
label variable oppHarass "government harassed opposition"

** Allegations of media bias
gen mediaBias = 1 if nelda16 == "yes"
replace mediaBias = 0 if mediaBias == .
label variable mediaBias "allegations of media bias"

** Good economic growth
gen goodGrow = 1 if nelda17 == "yes"
replace goodGrow = 0 if goodGrow == .
label variable goodGrow "economic growth said to be good"

** Economic crisis
gen econCrisis = 1 if nelda18 == "yes"
replace econCrisis = 0 if econCrisis == .
label variable econCrisis "country said to be in economic crisis"

** Large recipient of outside aid
gen recAid = 1 if nelda19 == "yes"
replace recAid = 0 if recAid == .
label variable recAid "country said to be large recipient of outside aid"

** Office of incumbent contested in election
gen incContest = 1 if nelda20 == "yes"
replace incContest = 0 if incContest == .
label variable incContest "Office of incumbent contested in election"

** Incumbent ran
gen incRun = 1 if nelda21 == "yes"
replace incRun = 0 if incRun == .
label variable incRun "incumbent ran in election"

** If incumbent didn't run, was there a chosen successor?
gen incSucc = 1 if nelda22 == "yes"
replace incSucc = 0 if incSucc == .
label variable incSucc "chosen successor ran"

** If successor ran, did successor win election
gen succWin = 1 if nelda23 == "yes"
replace succWin = 0 if succWin == .
label variable succWin "if successor ran, did win"

** Alternative incumbent party win variable that includes 12 cases of successor winning
gen incosWin = incWin
replace incosWin = 1 if succWin == 1 & (incWin == 0|incWin == .)
label variable incosWin "Incumbent party of chosen successor wins election"

** Reliable polls?
gen pollExist = 1 if nelda25 == "yes"
replace pollExist = 0 if pollExist == .
label variable pollExist "reliable polls present"

** If reliable polls, favorable to incumbent?
gen incApp = 1 if nelda26 == "yes"
replace incApp = 0 if incApp == .
label variable incApp "Are reliable polls favorable to incumbent"

** Opposition gains in vote count?
gen oppInc = 1 if nelda27 == "yes"
replace oppInc = 0 if oppInc == .
label variable oppInc "did opposition gain ground in election"

** Did population hear criticism of election?
gen popFraud = 1 if nelda28 == "yes"
replace popFraud = 0 if popFraud == .
label variable popFraud "Population heard criticism of handling of election"

** Riots after elections?
gen riots = 1 if nelda29 == "yes"
replace riots = 0 if riots == .
label variable riots "riots after elections"

** Riots over voter fraud.
gen riotFraud = 1 if nelda30 == "yes"
replace riotFraud = 0 if riotFraud == .
label variable riotFraud "riots over voter fraud"

** Violent response against protestors
gen violResp = 1 if nelda31 == "yes"
replace violResp = 0 if violResp == .
label variable violResp "violent government response to riots"

** Results favoring opposition cancelled
gen oppCanc = 1 if nelda32 == "yes"
replace oppCanc = 0 if oppCanc == .
label variable oppCanc "results favoring opposition cancelled"

** Significant civilian violence before elections
gen violPrior = 1 if nelda33 == "yes"
replace violPrior = 0 if violPrior == .
label variable violPrior "significant civilian violence prior to election"

** Results favorable to incumbent cancelled
gen incCanc = 1 if nelda34 == "yes"
replace incCanc = 0 if incCanc == .
label variable incCanc "results favoring incumbent cancelled"

** Was incCanc due to widespread protest
gen incCancProt = 1 if nelda35 == "yes"
replace incCancProt = 0 if incCancProt == .
label variable incCancProt "Was incCanc because of protests"

** If incCanc were new elections held
gen incCancNew = 1 if nelda37 == "yes"
replace incCancNew = 0 if incCancNew == .
label variable incCancNew "if incCanc new elections held"

** If incCancNew were results different
gen incCancNewDif = 1 if nelda38 == "yes"
replace incCancNewDif = 0 if incCancNewDif == .
label variable incCancNewDif "if incCancNew were results different"

** incumbent replaced
gen incRep = 1 if nelda39 == "yes"
replace incRep = 0 if incRep == .
label variable incRep "incumbent was replaced"

** incumbent replaced because of vote count
gen incRepVote = 1 if nelda40 == "yes"
replace incRepVote = 0 if incRepVote == .
label variable incRepVote "incumbent replaced because of vote count"

** incumbent replaced because of widespread protests
gen incRepProt = 1 if nelda41 == "yes"
replace incRepProt = 0 if incRepProt == .
label variable incRepProt "incumbent replaced because of widespread protests"

** coup prevented elected leader from taking power
gen incRepCoup = 1 if nelda42 == "yes"
replace incRepCoup = 0 if incRepCoup == .
label variable incRepCoup "coup prevented elected leader from gaining office"

** international monitors present
gen intMonitor = 1 if nelda45 == "yes"
replace intMonitor = 0 if intMonitor == .
label variable intMonitor "international monitors present"

** Western monitors present
gen westMonitor = 1 if nelda46 == "yes"
replace westMonitor = 0 if westMonitor == .
label variable westMonitor "Western monitors present"

** Western allegations of fraud
gen westMonFraud = 1 if nelda47 == "yes"
replace westMonFraud = 0 if westMonFraud == .
label variable westMonFraud "Western monitors charged fraud"

** good relations with US
gen goodUS = 1 if nelda50 == "yes"
replace goodUS = 0 if goodUS == .
label variable goodUS "country has good relations with US"

** substantial Western links
gen westLink = 1 if nelda53 == "yes"
replace westLink = 0 if westLink == .
label variable westLink "country has substantial military, economic, or political links with West or IGO"

** Merge in CPI data
merge m:1 ccode year using ".\cpi(merge).dta"
drop if _merge == 2
drop _merge

** Merge in Economic growth data
merge m:1 ccode year using ".\polity2012(formerge).dta"
drop if _merge == 2
drop _merge
merge m:1 ccode year using ".\QOGecondata123(rawmerge).dta"
drop if _merge == 2
drop _merge


* Creating cut data (i.e. only elections considered "competitive")
keep if oppAllow == 1 & multLegal == 1 & multChoice == 1
drop if incWin == .
* drop if _n == 18 | _n == 20 | _n == 22 | _n == 24 | _n == 40 | _n == 43 | _n == 73 | _n == 95 | _n == 97 | _n == 101 | _n == 120 | _n == 123 | _n == 125 | _n == 127 | _n == 129 | _n == 131 | _n == 133 | _n == 140 | _n == 143 | _n == 145 | _n == 151 | _n == 155 | _n == 157 | _n == 159 | _n == 191 | _n == 193 | _n == 195 | _n == 216 | _n == 219
gen politystd = (polity2l1 + 10)/20


* Tag Latin American Countries for OSI
* merge m:1 ccode year using "C:\Documents and Settings\rkennedy\My Documents\My Dropbox\Public\LA Elections\GDP Data\regions.dta"
* drop if nelda1 == ""
* drop _merge
* gen laCountry = 1 if ht_region2 == 2
* label variable laCountry "Latin American country"
* replace laCountry = 0 if laCountry == .




